import { SkillModel } from '../../models/Skill'

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event)
    
    // 验证必填字段
    if (!body.name) {
      return {
        success: false,
        message: '技能名称不能为空',
        data: null
      }
    }
    
    if (!body.category_id) {
      return {
        success: false,
        message: '分类ID不能为空',
        data: null
      }
    }
    
    const skillId = await SkillModel.createSkill({
      category_id: parseInt(body.category_id),
      name: body.name,
      level: body.level || 'intermediate',
      color: body.color || '#1890ff',
      description: body.description || '',
      status: body.status || 'active',
      sort_order: body.sort_order || 0
    })
    
    return {
      success: true,
      message: '创建技能成功',
      data: { id: skillId }
    }
  } catch (error: any) {
    console.error('创建技能失败:', error)
    
    return {
      success: false,
      message: `创建技能失败: ${error.message}`,
      data: null
    }
  }
})